﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MxitAPI;

namespace MxitGUI {
    public partial class RegisterClientWindow : Form {

        private string userName;
        public bool Success { get; set; }

        public RegisterClientWindow( string userName ) {
            InitializeComponent();

            this.userName = userName;
            Init();
        }

        private void Init() {
            GetChallengeHandler handler = new GetChallengeHandler();
            handler.Execute();

            ChallengeData challengeData = MxitAPI.ApplicationInfo.Instance.ChallengeData;
            Image image = Utilities.Base64ToImage( System.Text.ASCIIEncoding.ASCII.GetString( challengeData.Captcha ) );

            lblCaptcha.Width = image.Width;
            lblCaptcha.Height = image.Height;
            lblCaptcha.Text = string.Empty;
            lblCaptcha.Image = image;
        }

        private void btnRegisterClient_Click( object sender, EventArgs e ) {
            ApplicationInfo app = ApplicationInfo.Instance;
            ProductIdRequestData pidRequestData = new ProductIdRequestData();
            pidRequestData.CaptchaResponse = txtCaptcha.Text;
            pidRequestData.UserName = userName;
            pidRequestData.CountryCode = "ZA";
            pidRequestData.Locale = "en";
            pidRequestData.SessionId = app.ChallengeData.SessionId;
            pidRequestData.Url = app.ChallengeData.Url;

            GetProductIdHandler handler = new GetProductIdHandler( pidRequestData );
            handler.Execute();

            /*Console.WriteLine( "PIDErrorCode " + app.PIDErrorCode );
            Console.WriteLine( "ProductId " + app.ProductId );
            Console.WriteLine( "PrimaryTcpConnectionString " + app.PrimaryTcpConnectionString );
            Console.WriteLine( "PrimaryHttpConnectionString " + app.PrimaryHttpConnectionString );
            Console.WriteLine( "DialingCode " + app.DialingCode );
            Console.WriteLine( "NationalPrefix " + app.NationalPrefix );
            Console.WriteLine( "InterationalPrefix " + app.InterationalPrefix );
            Console.WriteLine( "SecondaryTcpConnectionString " + app.SecondaryTcpConnectionString );
            Console.WriteLine( "SecondaryHttpConnectionString " + app.SecondaryHttpConnectionString );
            Console.WriteLine( "KeepAliveTime " + app.KeepAliveTime );
            Console.WriteLine( "KeepAliveTime " + app.KeepAliveTime );
            Console.WriteLine( "LoginName " + app.UserName );
            Console.WriteLine( "CountryCode " + app.CountryCode );
            Console.WriteLine( "Region " + app.Region );
            Console.WriteLine( "utf8Disabled " + app.utf8Disabled );*/
            
            // Set the success property to true if the product id was resturned
            DialogResult = DialogResult.OK;
            if ( app.PIDErrorCode == 0 )
                Success = true;
            else
                Success = false;

            Close();

        }

        private void btnCancel_Click( object sender, EventArgs e ) {
            DialogResult = DialogResult.Cancel;
            Close();
        }


    }
}
